package com.zkingsoft.actions.admin;

import java.io.UnsupportedEncodingException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.zkingsoft.model.ClassCost;
import com.zkingsoft.model.PrizeCount;
import com.zkingsoft.model.Statement;
import com.zkingsoft.plugin.pojo.PaginationVO;
import com.zkingsoft.plugin.util.DateUtils;
import com.zkingsoft.plugin.util.ExcelUtil;
import com.zkingsoft.plugin.util.ExcelUtil.ViewExcel;
import com.zkingsoft.pojo.AjaxResult;
import com.zkingsoft.services.ClassCostService;

/**
 * 统计管理--班级成本表
 * @author Administrator
 *
 */
@Controller
@RequestMapping(value ="admin/classCost")
public class ClassCostController {
	@Resource
	ClassCostService classCostService;
	
	/**
	 * 显示列表
	 * */
	@RequestMapping(value = "inPage")
	public @ResponseBody AjaxResult findinPage(ClassCost classCost,PaginationVO pageVo){
		if (pageVo == null) {
			pageVo = new PaginationVO();
		}
		List<ClassCost> list =classCostService.findInPage(classCost, pageVo);
		for(ClassCost s : list){
			if(s.gettInTuition()==null){
				s.settInTuition(0.0);
				s.settUnTuition(s.getPayable()-s.gettInTuition());
			}
		}
		AjaxResult result = new AjaxResult(AjaxResult.STATUS_OK,null,null,list,classCostService.findTotal(classCost));
		return result;
	}
	
	/**
	 * 显示列表（对象）
	 * */
	@RequestMapping(value = "model")
	public @ResponseBody AjaxResult findModel(ClassCost classCost){
		List<ClassCost> list =classCostService.findModel(classCost);
		for(ClassCost s : list){
			if(s.gettInTuition()==null){
				s.settInTuition(0.0);
				s.settUnTuition(s.getPayable()-s.gettInTuition());
			}
		}
		AjaxResult result = new AjaxResult(AjaxResult.STATUS_OK,null,null,list,classCostService.findTotal(classCost));
		return result;
	}
	
	
	/**
 	 * 
 	 * @Description:导出excel
 	 * @author:laotie
 	 * @return
 	 * 返回类型 ModelAndView
 	 * @date 2017年5月11日
 	 */
 	@RequestMapping(value = "/exportExcel")
 	public @ResponseBody ModelAndView report(ModelMap model, HttpServletRequest request, HttpServletResponse response,
 			ClassCost classCost) {
 	    try {
 	        if (classCost.getClaName() != null && !(classCost.getClaName().equals(""))) {
 	        	classCost.setClaName(java.net.URLDecoder.decode(classCost.getClaName(), "UTF-8"));
 	        }
 	    } catch (UnsupportedEncodingException e1) {
 	        e1.printStackTrace();
 	    }
 	    // 查询符合条件的数据
 	    List<ClassCost> dataList = classCostService.findModel(classCost);
 	    //把数据组装成我们要的数据，外面的list表示一行数据，也就是一个对象的信息，对应到excel的一行数据。里面的list表示对象的一个属性，对应excel的单元格
 	    SimpleDateFormat sdf = new SimpleDateFormat(DateUtils.DATE_FORMAT_DD);
 	    List<List< Object>> list = new ArrayList<>();
 	        for (ClassCost obj : dataList) {
 	            List<Object> temp = new ArrayList<>();
 	            temp.add(obj.getStuNo());
 	            temp.add(obj.getStuName());
 	            temp.add(obj.getStuSchool());
 	            temp.add(obj.getStuIfschool());
 	            temp.add(obj.getClaName());
 	            temp.add(obj.getStuStatus());
 	            temp.add(obj.getPayable());
 	            temp.add(obj.gettInTuition());
 	            temp.add(obj.gettUnTuition());
 	            temp.add(obj.getBiMoney());
 	            temp.add(obj.getMaMoney());
 	            temp.add(obj.getTuiMoney());
 	            temp.add(obj.getpLoss());
 	            temp.add(obj.getStuRemark());
 	            list.add(temp);
 	        }
 	         
 	        //调用我们的excel工具类，导出数据
 	    HSSFWorkbook workbook = ExcelUtil.generateExcel("班级收入成本列表",
 	            new String[] { "学号","学生姓名","来自院校","是否院校毕业","班级","状态","应交学费","已交学费","未交学费",
 	            		"已领奖金金额","管理费金额","退费金额","盈亏金额","备注"}, list);
 	    try {
 	        ViewExcel.buildExcel(null, workbook, request, response, "班级收入成本列表.xls");
 	    } catch (Exception e) {
 	        e.printStackTrace();
 	    }
 	    return new ModelAndView(new ViewExcel(), model);
 	}
}
